<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class Studio extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $rel = Db::field('lc.cinema, ls.*')
                ->table(['l_cinema'=>'lc', 'l_studio'=>'ls'])
                ->where('lc.id=ls.cinema_id')
                ->select();

        return view('Studio/index', [
            'data'=>$rel
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = Db::table('l_cinema')
                    ->select();

        return view('Studio/add', [
            'data'=>$data
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $rel = $request->post();

        $list = Db::table('l_studio')
                ->where('cinema_id', $rel['cinema_id'])
                ->where('studio_name', $rel['studio_name'])
                ->select();
        if($list){
            $this->error('该影院里已有此影厅');
            exit;
        }

        $seat = "'__eeeeeeee','eeeeeeeeee','eeeeeeeeee','eeeeeeeeee','eeeeeeeeee','eeeeeeeeee','eeeeeeeeee','eeeeeeeeee','eeeeeeeeee'";
        $data = [
            'cinema_id'=>$rel['cinema_id'],
            'studio_name'=>$rel['studio_name'],
            'studio_type'=>$rel['type'],
            'studio_seat'=>$seat
        ];

        $result = Db::table('l_studio')
                    ->insert($data);

        if($result){
            $this->success('添加影厅成功', 'admin/Studio/index');
        }else{
            $this->error('添加失败,请重试');
        }

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $data = Db::table('l_cinema')
                ->select();

        $list = Db::table('l_studio')
                ->where('id', $id)
                ->find();

        return view('Studio/edit', [
            'data'=>$data,
            'list'=>$list
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $rel = $request->post();
        $data = [
            'cinema_id'=>$rel['cinema_id'],
            'studio_name'=>$rel['studio_name'],
            'studio_type'=>$rel['type']
        ];

        $list = Db::table('l_studio')
                ->where('id', $id)
                ->update($data);

        if($list){
            $this->success('修改影厅成功', 'admin/Studio/index');
        }else{
            $this->error('修改影厅失败,请重试');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = Db::table('l_studio')
            ->where('id', $id)
            ->delete();

        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . ' 的影厅删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . ' 的影厅删除失败,请重试!';
        }
        return json($info);
    }
}
